package org.microservice.common.util;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.oauth2.provider.authentication.OAuth2AuthenticationDetails;

import java.util.Collection;

/**
 * Created with IntelliJ IDEA.
 * User: 齐丹枫
 * Date: 2018/6/29
 * Time: 10:54
 * To change this template use File | Settings | File Templates.
 * Description:
 */
public class AuthUtil {


    public static String getAuthorities(){
        Collection<? extends GrantedAuthority> authorities =
                SecurityContextHolder.getContext().getAuthentication().getAuthorities();
        return authorities.toString();
    }



    public static String getUserName(){

        String userName = (String)SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        return userName; //.substring(0,userName.indexOf("#"))
    }


    public static  String getTokenValue(){
        OAuth2AuthenticationDetails details = (OAuth2AuthenticationDetails) SecurityContextHolder.getContext().getAuthentication().getDetails();
        return details.getTokenValue();
    }

  /*  public static int getUserId(){
        String userName = (String)SecurityContextHolder.getContext().getAuthentication().getPrincipal();

        return Integer.parseInt(userName.substring(userName.indexOf("#")+1,userName.length()));
    }*/

}
